package com.inspur.gs.sbssdl.gzgl.ssqyj.ssqyj.back.bizactions;
import com.inspur.edp.bef.api.exceptions.BizMessageException;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.util.ArrayList;

public class gzssqyjlockslMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String id;
	public gzssqyjlockslMgrAction(IBEManagerContext managerContext, String id) {
		super(managerContext);
		this.id = id;
	}

    // 所属权
    private String ssqyjCfg = "com.inspur.gs.sbssdl.gzgl.ssqyj.ssqyj.back.gzssqyj";

    // 工装台账
    private String gztzCfg = "com.inspur.gs.sbssdl.gzgl.ledger.ledger.back.FrockLedger";

    /**
     * 锁定数量
     * 1.增加移交履历日志
     * 2.更新锁定数量
     */
	@Override
	public void execute() {
        IStandardLcp ssqLcp = LcpUtil.getLcp(ssqyjCfg);
		IStandardLcp gztzLcp = LcpUtil.getLcp(gztzCfg);

		IEntityData ssqData = ssqLcp.retrieve(this.id).getData();
		IEntityDataCollection ssqMx = ssqData.getChilds().get("yjmx");

		ssqMx.forEach((v)->{
			lockKc(ssqData,v,gztzLcp);
		});

    }
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}

	// 锁定库存
	private void lockKc (IEntityData ssqData,IEntityData mx,IStandardLcp gzLcp){
		String mxId = mx.getID();
		String yjdh = ssqData.getValue("sqdh").toString();
		// 校验当前数量是否满足扣除
		IValuesContainer gzIdAcc = (IValuesContainer) mx.getValue("gzid");
		if (gzIdAcc == null) {
			return;
		}
		if (gzIdAcc.getValue("gzid") == null || gzIdAcc.getValue("gzid").toString().isEmpty()) {
			return;
		}
		String gzId = gzIdAcc.getValue("gzid").toString();
		IEntityData gzData = gzLcp.retrieve(gzId).getData();
		if (gzData == null) {
			return;
		}

		// 账面数量 锁定数量 明细申请数量
		int zmsl = Integer.parseInt(gzData.getValue("zmsl").toString());
		int sdsl = Integer.parseInt(gzData.getValue("sdsl").toString());
		int mxSl = Integer.parseInt(mx.getValue("sl").toString());
		if (zmsl < mxSl + sdsl){
			IBizMessage mes = new BizMessage();
			mes.setLevel(MessageLevel.Error);
			MessageLocation location = new MessageLocation();
			location.getDataIds().add(mxId);
			location.setNodeCode(yjdh);
			mes.setLocation(location);
			mes.setMessageFormat(gzData.getValue("bzmc") + ":申请数量大于库存剩余数量,请刷新页面并重新填写数量!");
			throw new BizMessageException("库存不足!", mes);
		}

		// 检查扣减日志是否存在,存在则返回
		IEntityDataCollection yjjls = gzData.getChilds().get("yjjl");
		boolean hasItem = yjjls.stream().anyMatch(item -> item.getValue("yjmxId") != null && item.getValue("yjmxId").toString().equals(mxId));
		if (hasItem) return;

		// 锁定数量
		ModifyChangeDetail gzMcd = new ModifyChangeDetail(gzId);
		gzMcd.putItem("sdsl",mxSl + sdsl);

		// 生成日志
		ArrayList<String> nodeCodes = new ArrayList<>();
		nodeCodes.add("yjjl");
		ArrayList<String> hierarchyIds = new ArrayList<>();
		hierarchyIds.add(gzId);
		IEntityData gzChild = gzLcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

		// 生成扣减日志
		ModifyChangeDetail mxDetail = new ModifyChangeDetail(gzChild.getID());
		mxDetail.putItem("spzt","0");//待审核
		mxDetail.putItem("yjdh",yjdh);//单号
		mxDetail.putItem("yjid",ssqData.getID());
		mxDetail.putItem("yjmxId",mxId);

		gzMcd.addChildChangeSet("yjjl",mxDetail);

		// 提交
		gzLcp.modify(gzMcd);
		gzLcp.save();
	}
}